package com.fmdj.odr.service.impl;

import cn.hutool.core.codec.Base64;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.odr.db.dao.OrderCommentDao;
import com.fmdj.odr.db.dao.OrderDao;
import com.fmdj.odr.db.pojo.OrderCommentEntity;
import com.fmdj.odr.service.OrderCommentService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.model.ciModel.auditing.AuditingJobsDetail;
import com.qcloud.cos.model.ciModel.auditing.TextAuditingRequest;
import com.qcloud.cos.model.ciModel.auditing.TextAuditingResponse;
import com.qcloud.cos.region.Region;
import io.protostuff.runtime.ArraySchemas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class OrderCommentServiceImpl implements OrderCommentService {

    @Autowired
    private OrderCommentDao orderCommentDao;

    @Autowired
    private OrderDao orderDao;

    @Value("${tencent.cloud.secretId}")
    private String secretId;

    @Value("${tencent.cloud.secretKey}")
    private String secretKey;

    @Value("${tencent.cloud.region-public}")
    private String regionPublic;

    @Value("${tencent.cloud.bucket-public}")
    private String bucketPublic;

    @Override
    @Transactional
    @LcnTransaction
    public int insertComment(OrderCommentEntity entity) {
        //校验该订单是否与司机乘客相关联
        HashMap map = new HashMap<>(){{
            put("orderId", entity.getOrderId());
            put("driverId", entity.getDriverId());
            put("customerId", entity.getCustomerId());
        }};
        long count = orderDao.validDriverAndCustomerOwnOrder(map);
        if (count!=1) {
            throw  new GlobalException("该订单与司机乘客无关联");
        }

        //使用数据万象服务审核评论
        BasicCOSCredentials cred = new BasicCOSCredentials(secretId, secretKey); //身份凭证
        Region region = new Region(regionPublic); //地域信息
        ClientConfig config = new ClientConfig(region); //客户端配置
        COSClient client = new COSClient(cred, config);//连接客户端

        TextAuditingRequest request = new TextAuditingRequest();
        request.setBucketName(bucketPublic);
        request.getInput().setContent(Base64.encode(entity.getRemark()));
        request.getConf().setDetectType("all");

        //发起文本审核请求，并获取响应结果
        TextAuditingResponse response = client.createAuditingTextJobs(request);
        AuditingJobsDetail detail = response.getJobsDetail();
        String state = detail.getState();
        if ("Success".equalsIgnoreCase(state)) { //审核成功(审核任务结束)
            //获取审核结果
            String result = detail.getResult();
            if (!"0".equals(result)) {
                //若审核结果不成功
                entity.setRemark(null);
            }
        }
        
        //订单评论插入到数据路
        int rows = orderCommentDao.insert(entity);
        if (rows !=1) {
            throw new GlobalException("订单评论记录插入失败");
        }

        return rows;
    }

    @Override
    public HashMap searchCommentByOrderId(Map map) {
        HashMap hashMap = orderCommentDao.searchCommentByOrderId(map);
        return hashMap;
    }
}
